package com.company.beauty.geo.util;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.company.beauty.geo.config.properties.GeocodeProperties;
import com.company.beauty.geo.constants.GeoCodeConstants;
import com.company.beauty.geo.request.RgeoRequest;
import com.company.beauty.geo.response.GeoResponse;
import com.company.beauty.geo.response.RgeoResponse;
import com.company.nuwa.common.enumration.GlobalCallbackEnum;
import com.company.nuwa.common.enumration.YesOrNoEnum;
import com.company.nuwa.common.request.GeoRequest;
import com.company.nuwa.common.utils.JsonUtils;
import com.company.nuwa.common.utils.Preconditions;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;
import java.util.Objects;

/**
 * 高德地图API
 *
 * @author youyoukele
 * @version 1.0
 * @date 2024/04/22
 */
@Slf4j
public class GeocodeMapUtil {

    private static final GeocodeProperties GEOCODE_PROPERTIES = SpringUtil.getBean(GeocodeProperties.class);

    /**
     * 逆地理编码
     *
     * @param request 参数
     * @return RgeoResponse
     */
    public static RgeoResponse rgeo(RgeoRequest request) {
        /**
         * 经纬度坐标
         * 传入内容规则：经度在前，纬度在后，经纬度间以“,”分割，经纬度小数点后不要超过 6 位。
         * 如果需要解析多个经纬度的话，请用"|"进行间隔，并且将 batch 参数设置为 true，最多支持传入 20 对坐标点。每对点坐标之间用"|"分割。
         */
        String url = MessageFormat.format(GeoCodeConstants.RGEO_URL, GEOCODE_PROPERTIES.getKey(),
                request.getLng(), request.getLat());
        HttpRequest httpRequest = HttpUtil.createGet(url);
        log.info("[逆地理编码],request:{}", JsonUtils.objectToJson(httpRequest));
        HttpResponse httpResponse = httpRequest.execute();
        log.info("[逆地理编码],request:{},httpResponse:{}", httpRequest, httpResponse);
        Preconditions.checkArgument(httpResponse.isOk(), GlobalCallbackEnum.BAD_REQUEST.getCode(),
                "逆地理编码异常");
        String body = httpResponse.body();
        log.info("[逆地理编码],request:{},result:{}", httpRequest, body);
        RgeoResponse rgeoResponse = JsonUtils.jsonToObject(body, RgeoResponse.class);
        log.info("[逆地理编码],geocodeRgeoResponse:{}", JsonUtils.objectToJson(rgeoResponse));
        Preconditions.checkArgument(Objects.nonNull(rgeoResponse),
                GlobalCallbackEnum.BAD_REQUEST.getCode(), "逆地理编码异常");
        return rgeoResponse;
    }

    /**
     * 地理编码
     *
     * @param request 参数
     * @return GeoResponse
     */
    public static GeoResponse geo(GeoRequest request) {
        String url = MessageFormat.format(GeoCodeConstants.GEO_URL, GEOCODE_PROPERTIES.getKey(),
                request.getAddress(), request.getCity());
        HttpRequest httpRequest = HttpUtil.createGet(url);
        log.info("[地理编码],request:{}", JsonUtils.objectToJson(httpRequest));
        HttpResponse httpResponse = httpRequest.execute();
        log.info("[地理编码],request:{},httpResponse:{}", httpRequest, httpResponse);
        Preconditions.checkArgument(httpResponse.isOk(), GlobalCallbackEnum.BAD_REQUEST.getCode(),
                "地理编码异常");
        String body = httpResponse.body();
        log.info("[地理编码],request:{},result:{}", httpRequest, body);
        GeoResponse geoResponse = JsonUtils.jsonToObject(body, GeoResponse.class);
        log.info("[地理编码],geocodeRgeoResponse:{}", JsonUtils.objectToJson(geoResponse));
        Preconditions.checkArgument(Objects.nonNull(geoResponse)
                        && geoResponse.getStatus()
                        .equals(String.valueOf(YesOrNoEnum.YES.getCode())),
                GlobalCallbackEnum.BAD_REQUEST.getCode(), "地理编码异常");
        return geoResponse;
    }
}